package org.spring.springboot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.junit.platform.commons.util.StringUtils;
import org.spring.springboot.entity.SystemManage;
import org.spring.springboot.po.SystemManagePO;
import org.spring.springboot.result.R;
import org.spring.springboot.result.Result;
import org.spring.springboot.server.SystemManageService;
import org.spring.springboot.util.ConstantUtil;
import org.spring.springboot.util.MD5Util;
import org.spring.springboot.vo.SystemManageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "系统管理")
@RequestMapping("/systemManage")
public class SystemManageController {
    @Autowired
    private SystemManageService systemManageService;

    @PostMapping("/addSystemUser")
    @ApiOperation(value = "新增管理员")
    public Result addSystemUser(@RequestBody SystemManageVO systemManageVO){
        systemManageService.addSystemUser(systemManageVO);
        return Result.newSuccessResult(systemManageVO);
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改管理员密码")
    public Result updatePassword(@RequestBody SystemManageVO systemManageVO){
        systemManageService.updatePassword(systemManageVO);
        return Result.newSuccessResult(systemManageVO);
    }

    @PostMapping("/deleteSystemUser")
    @ApiOperation(value = "删除管理员")
    public Result deleteSystemUser(@RequestParam(value = "id") Integer id){
        systemManageService.deleteSystemUser(id);
        return Result.newSuccessResult("删除id:"+id);
    }

    @PostMapping("/getSystemNameList")
    @ApiOperation(value = "查询管理员姓名")
    public R SystemNameList(Integer pageNum, Integer pageSize){
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> maps = systemManageService.getSystemNameList();
        Long total = page.getTotal();
        return R.newSuccessResult(maps,total);
    }

    @PostMapping("/updateUserId")
    @ApiOperation(value = "管理员禁用")
    public Result updateUserId(@RequestParam(value = "userid") Integer userid,
                               @RequestParam(value = "id") Integer id){
        systemManageService.updateUserId(userid, id);
        return Result.newSuccessResult("id为:"+id+" userid为:"+userid);
    }

    @PostMapping("/sysLogin")
    @ApiOperation(value = "系统用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
    })
    public Result<SystemManageVO> sysLogin(@RequestParam String username, @RequestParam String password) {
        return Result.newSuccessResult(systemManageService.sysLogin(username,password));
    }

    @PostMapping("/getSystemUserList")
    @ApiOperation(value = "查询管理员信息")
    public R SystemUserList(Integer pageNum, Integer pageSize){
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> maps = systemManageService.getSystemUserList();
        Long total = page.getTotal();
        return R.newSuccessResult(maps,total);
    }
}
